<?php
class WsBaseController extends Controller implements IWebServiceProvider {
	private $HEADER_NS = 'AUTHORISE';
	public $_session;
	//the headers namespace
	
	public function Authorise() {
	}
	
	public function init()
	{
		$this->_session=new CHttpSession;
	}
	
	public function beforeWebMethod($service)
	{
		if ($service->methodName !== 'login'){
			// $SOAP_HEADERS;$i=0;
			// // $session=new CHttpSession;
			// // $session->open();
			// // $sessionid = session_id();
			// // $session->sets
			// $rawPOST = file_get_contents('php://input');
			// $xml = simplexml_load_string($rawPOST);
			// $authinfo = $xml->xpath('//ns2:AUTHORISE/*');
			// // $authxml = simplexml_load_string($authinfo->asXML());
// 			
			// foreach($authinfo as $item){
				// // 'derefence' into a seperate xml tree for performance
				// $row = simplexml_load_string($item->asXML());
				// $SOAP_HEADERS[$i] = $row;
				// $i++;
			// }
			// if ($SOAP_HEADERS == null || count($SOAP_HEADERS)!=3)
				// throw new CException('Login required(0).');
			// else{
				// if ($SOAP_HEADERS[0]->value === '')
					// throw new CException('AC is null.Login required(1)');
				// else{
					// $session=new CHttpSession;
					// $session->open();
					// if ($session['authcode'] == $SOAP_HEADERS[0]->value)
						// return true;
					// else
						// throw new CException('AC is invalid.');
				// }
				// // $identity = new UserIdentity($SOAP_HEADERS[0]->value,
					// // $SOAP_HEADERS[1]->value);
				// // if($identity ->authenticatebyws()){
					// // // Yii::app()->user->login($identity);
					// // return true;
			// }
			return true;
		}
		
		$safeMethods=array(
			'login',
			'getUsers',
		);
		$pattern='/^('.implode('|',$safeMethods).')$/i';
		if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))
			return true;
		else
			throw new CException('Login required.');
	}
	
	/**
	 * This method is required by IWebServiceProvider.
	 * @param CWebService the currently requested Web service.
	 */
	public function afterWebMethod($service)
	{
	}
}
